﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using EasyToHelp.Business;
using EasyToHelp.Entity;
using EasyToHelp.Service;
using Microsoft.AspNetCore.Mvc;

namespace EasyToHelp.WAP.Controllers
{
    public class MerchantInfoController : BaseController
    {
        private readonly IMemberService Member;
        private readonly IMerchantInfoService Merchant;
        private readonly ICardInfoService CardInfo;
        private readonly IBusinessLicenseInfoService BusinessLicenseInfo;
        public MerchantInfoController(IMemberService _Member, IMerchantInfoService _Merchant, ICardInfoService _CardInfo, IBusinessLicenseInfoService _BusinessLicenseInfo)
        {
            Member = _Member;
            Merchant = _Merchant;
            CardInfo = _CardInfo;
            BusinessLicenseInfo = _BusinessLicenseInfo;
        }
        public IActionResult Index()
        {
            return View();
        }
        /// <summary>
        /// 商户认证信息
        /// </summary>
        /// <returns></returns>
        public IActionResult MerchantInfo(int Id)
        {
            var m = Merchant.GetList().Where(x => x.UserId == UserId()).FirstOrDefault();
            if (m == null)
            {
                return View();
            }
            else
            {
                m.CardInfo = CardInfo.GetList().Where(x => x.MerchantId == m.Id).FirstOrDefault();
                m.BusinessLicenseInfo = BusinessLicenseInfo.GetList().Where(x => x.MerchantId == m.Id).FirstOrDefault();
                return View(m);
            }
        }
        #region 商户信息操作
        /// <summary>
        /// 商户信息操作
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IActionResult EditMerchant(MerchantInfo model)
        {
            if (string.IsNullOrWhiteSpace(model.Name))
            {
                return Json(ResultInfo.Error("请输入名字！"));
            }
            var m = Merchant.GetList().Where(x => x.Name == model.Name).FirstOrDefault();
            if (m != null)
            {
                return Json(ResultInfo.Error("该商户已存在！"));
            }
            if (string.IsNullOrWhiteSpace(model.Birthday.ToString()))
            {
                return Json(ResultInfo.Error("请选择生日！"));
            }
            if (string.IsNullOrWhiteSpace(model.Mobile))
            {
                return Json(ResultInfo.Error("请输入联系方式！"));
            }
            if (string.IsNullOrWhiteSpace(model.Address))
            {
                return Json(ResultInfo.Error("请输入详细住址！"));
            }
            if (model.Id > 0)
            {
                if (Merchant.Update(model) > 0)
                {
                    return Json(ResultInfo.Success("编辑成功！"));
                }
                else
                {
                    return Json(ResultInfo.Error("编辑失败！"));
                }
            }
            else
            {
                model.AddTime = DateTime.Now;
                model.UserId = UserId();
                if (Merchant.Add(model) > 0)
                {
                    return Json(ResultInfo.Success("提交成功！"));
                }
                else
                {
                    return Json(ResultInfo.Error("提交失败！"));
                }
            }
        }

        #endregion

        #region 身份证认证操作
        public IActionResult EditCardInfo(CardInfo model)
        {
            if (string.IsNullOrWhiteSpace(model.RealName))
            {
                return Json(ResultInfo.Error("请输入姓名！"));
            }
            if (string.IsNullOrWhiteSpace(model.CardNo))
            {
                return Json(ResultInfo.Error("请输入身份证号！"));
            }
            if (string.IsNullOrWhiteSpace(model.Positive))
            {
                return Json(ResultInfo.Error("请上传正面图片！"));
            }
            if (string.IsNullOrWhiteSpace(model.Opposite))
            {
                return Json(ResultInfo.Error("请上传反面图片！"));
            }
            if (model.MerchantId==0)
            {
                return Json(ResultInfo.Error("请先认证商户基本信息！"));
            }
            var b = CardInfo.GetList().Where(x => x.MerchantId == model.MerchantId).FirstOrDefault();
            if (b != null)
            {
                model.Id = b.Id;
                model.AddTime = DateTime.Now;
                if (CardInfo.Update(model) > 0)
                {
                    return Json(ResultInfo.Success("更新成功！"));
                }
            }
            else
            {
                model.AddTime = DateTime.Now;
                if (CardInfo.Add(model) > 0)
                {
                    return Json(ResultInfo.Success("提交成功！"));
                }
            }
            return Json(ResultInfo.Error("操作失败！"));
        }
        #endregion

        #region 营业执照操作
        /// <summary>
        /// 营业执照操作
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public IActionResult EditBusinessLicenseInfo(BusinessLicenseInfo model)
        {
            if (string.IsNullOrWhiteSpace(model.LegalPerson))
            {
                return Json(ResultInfo.Error("请输入法人！"));
            }
            if (string.IsNullOrWhiteSpace(model.TermValidity.ToString()))
            {
                return Json(ResultInfo.Error("请选择过期时间！"));
            }
            if (string.IsNullOrWhiteSpace(model.LicensePIc))
            {
                return Json(ResultInfo.Error("请上传执照图片！"));
            }
            if (model.MerchantId == 0)
            {
                return Json(ResultInfo.Error("请先认证商户基本信息！"));
            }
            var b = BusinessLicenseInfo.GetList().Where(x => x.MerchantId == model.MerchantId).FirstOrDefault();
            if (b != null)
            {
                model.Id = b.Id;
                if (BusinessLicenseInfo.Update(model) > 0)
                {
                    return Json(ResultInfo.Success("更新成功！"));
                }
            }
            else
            {
                if (BusinessLicenseInfo.Add(model) > 0)
                {
                    return Json(ResultInfo.Success("提交成功！"));
                }
            }
            return Json(ResultInfo.Error("操作失败！"));
        }
        #endregion
    }
}